package com.hyjx.framework.component.field;

import com.hyjx.framework.orm.dao.BaseDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 扩展列信息服务实现类
 */
@Service
@Transactional
public class ExtFieldServiceImpl implements ExtFieldService {
    @Resource
    private BaseDao dao;

    @Override
    public List<ExtField> getFieldsByTableName(String tableName) {
        return dao.queryObjectList((new ExtField()).getModuleName() + ".query", tableName);
    }

    @Override
    public boolean update(Map<String, String> param) {
        String tableName = param.get("_tableName");
        String sql = "";
        for (Map.Entry<String, String> entry : param.entrySet()) {
            if ("_tableName".equals(entry.getKey()) || "_pkName".equals(entry.getKey()) || "_dataId".equals(entry.getKey()))
                continue;

            if ("".equals(sql)) {
                sql = entry.getKey() + " = '" + entry.getValue() + "'";
            } else {
                sql = sql + "," + entry.getKey() + " = '" + entry.getValue() + "'";
            }
        }

        if ("".equals(sql)) {
            return true;
        } else {
            sql = "update " + tableName + " set " + sql + " where " + param.get("_pkName") + "='" + param.get("_dataId") + "'";
            boolean flag = dao.executeSql(sql);
            return flag;
        }
    }
}